﻿using System;
using AppCommon;
using Controller;
using Model;

public partial class admincenter_HomeAdmin : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // Kiem tra session
            if (Session[Constants.S_ADMIN_USER] != null)
            {
                // Ok
            }
            else
            {
                // Nhan parameter tu form Login
                var userName = Request.Params["name"];
                var passWord = Request.Params["password"];

                if (userName != null && passWord != null)
                {
                    CheckLogin(userName, passWord);
                }
                else
                {
                    Response.Redirect("~/admincenter/");
                }                
            }            
        }
    }

    public void CheckLogin(string userName, string passWord)
    {
        var user = new User();
        user.UserName = userName;
        user.Password = passWord;

        var controller = new UserController();
        var listUser = controller.CheckExistsUser(user);

        if (listUser.Count <= 0)
        {
            Response.Redirect("~/admincenter/");
        }
        else
        {
            // Them vao Session
            Session.Add(Constants.S_ADMIN_USER, listUser[0]);
        }
    }
}